package com.mrdeer.web.service.sys.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mrdeer.common.entity.beans.PageEntity;
import com.mrdeer.common.entity.dto.MessageDTO;
import com.mrdeer.common.entity.sys.Message;
import com.mrdeer.common.exception.BusinessException;
import com.mrdeer.web.mapper.MessageMapper;
import com.mrdeer.web.service.sys.SysMessageService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

@Slf4j
@Service
@Transactional
public class SysMessageServiceImpl extends ServiceImpl<MessageMapper, Message> implements SysMessageService {

    @Resource
    private MessageMapper messageMapper;

    @Override
    public Message add(Message message) {
        Message select = getById(message.getId());
        if (null != select) {
            throw new BusinessException("The message exists ");
        }
        if (messageMapper.insert(message) > 0) {
            return messageMapper.selectById(message.getId());
        }
        throw new BusinessException("The message add failed");
    }

    @Override
    public boolean delete(String id) {
        Message message = getById(id);
        if (null == message) {
            throw new BusinessException("The message does not exist");
        }
        return messageMapper.deleteById(id) > 0;
    }

    @Override
    public Message update(Message message) {
        if (messageMapper.updateById(message) > 0) {
            Message select = messageMapper.selectById(message.getId());
            return select;
        }
        throw new BusinessException("the message update failed");
    }

    @Override
    public Message getById(String id) {
        return messageMapper.selectById(id);
    }

    @Override
    public Page<Message> list(MessageDTO dto) {
        if (null == dto.getPage()) {
            dto.setPage(new PageEntity());
        }
        return messageMapper.selectPage(dto.getPage().convert(), fuzzyQuery(dto));
    }

    @Override
    public LambdaQueryWrapper<Message> fuzzyQuery(MessageDTO dto) {
        LambdaQueryWrapper<Message> queryWrapper = Wrappers.<Message>query().lambda();
        if (null != dto) {
            if (StrUtil.isNotBlank(dto.getTitle())) {
                queryWrapper.like(Message::getTitle, dto.getTitle());
            }
            if (StrUtil.isNotBlank(dto.getType())) {
                queryWrapper.eq(Message::getType, dto.getType());
            }
            if (dto.getEndDate() != null) {
                queryWrapper.le(Message::getCreateTime, dto.getEndDate());
            }
            if (dto.getStartDate() != null) {
                queryWrapper.ge(Message::getCreateTime, dto.getStartDate());
            }
            if (StrUtil.isNotBlank(dto.getContent())) {
                queryWrapper.like(Message::getContent, dto.getContent());
            }
        }
        return queryWrapper;
    }

    @Override
    public List<Message> findByCreateSend(Boolean createSend) {
        MessageDTO dto = new MessageDTO();
        dto.setCreateSend(createSend);
        return messageMapper.selectList(fuzzyQuery(dto));
    }
}
